var Memberinfo = function() {

    var accountdetail_url = $('.accountdetail_url').val(),
        refundorder_url = $('.refundorder_url').val(),
        ordercommission_url = $('.ordercommission_url').val(),
        usercommission_url = $('.usercommission_url').val();
    var userId = $('.user_id').val();

    var table1 = $('#data-table1'),
        table2 = $('#data-table2'),
        table3 = $('#data-table3'),
        table4 = $('#data-table4');

    var isLoadTable1 = isLoadTable2 = isLoadTable3 = isLoadTable4 = false;

    var _init = function() {
        data_table1();
    };

    var data_table1 = function () {
        table1.dataTable({
            "processing": true,
            "ordering": false, // 禁止排序
            serverSide: true,// 开启服务器模式
            "ajax": {
                url: accountdetail_url,
                dataType: 'json',
                "complete":function(data){
                    var data = jQuery.parseJSON(data.responseText);
                    //console.log(data);
                    $('#accountdetail').find('.total_num').empty().html('累计消费次数：'+data.recordsTotal+'&nbsp;&nbsp;累计消费金额：'+data.totalNum+' 元');
                }
            },
            "autoWidth": false,
            "columns": [
                {"data": "orderNO"},
                {"data": "orderNum"},
                {"data": "money"},
                {"data": "userPhone"},
                {
                    "data": "create_time",
                    "render": function(data, type, row, meta) {
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    "data": "payDate",
                    "render": function(data, type, row, meta) {
                        if(data){
                            return moment(data).format('YYYY-MM-DD HH:mm');
                        }else{
                            return '';
                        }
                    }
                }
            ],
            "language": {
                url: '/public/static/assets/pages/Chinese.json'
            },
            "pageLength": 10,
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function(data) {
                var nTrs = table1.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });
        tableSearch(table1, {'userId':userId});
        isLoadTable1 = true;
    };

    var data_table2 = function () {
        table2.dataTable({
            "processing": true,
            "ordering": false, // 禁止排序
            serverSide: true,// 开启服务器模式
            "ajax": {
                url: refundorder_url,
                dataType: 'json',
                "complete":function(data){
                    var data = jQuery.parseJSON(data.responseText);
                    $('#refundorder').find('.total_num').empty().html('累计售后次数：'+data.recordsTotal+'&nbsp;&nbsp;累计退款金额：'+data.totalNum+' 元');
                }
            },
            "autoWidth": false,
            "columns": [
                {
                    "data": "imgUrl",
                    "render": function(data, type, row, meta) {
                        return '<img src="'+data+'" alt="" style="width: 40px;height: 40px;" />'
                    }
                },
                {"data": "title"},
                {"data": "count"},
                {
                    "data": "refund_fee",
                    "render": function(data, type, row, meta) {
                        return data / 100;
                    }
                },
                {
                    "data": "create_time",
                    "render": function(data, type, row, meta) {
                        data = new Date(data * 1000);
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    "data": "endDate",
                    "render": function(data, type, row, meta) {
                        if(data){
                            return moment(data).format('YYYY-MM-DD HH:mm');
                        }else{
                            return '';
                        }
                    }
                }
            ],
            "language": {
                url: '/public/static/assets/pages/Chinese.json'
            },
            "pageLength": 10,
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function() {
                var nTrs = table2.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });
        tableSearch(table2, {'userId':userId});
        isLoadTable2 = true;
    };

    var data_table3 = function () {
        table3.dataTable({
            "processing": true,
            "ordering": false, // 禁止排序
            serverSide: true,// 开启服务器模式
            "ajax": {
                url: ordercommission_url,
                dataType: 'json',
                "complete":function(data){
                    var data = jQuery.parseJSON(data.responseText);
                    $('#ordercommission').find('.total_num').empty().html('累计成交金额：'+data.totalNum+' 元&nbsp;&nbsp;累计收益金额：'+data.totalProportion+' 元');
                }
            },
            "autoWidth": false,
            "columns": [
                {"data": "userName"},
                {"data": "orderNO"},
                {
                    "data": "create_time",
                    "render": function(data, type, row, meta) {
                        if(data){
                            data = new Date(data * 1000);
                            return moment(data).format('YYYY-MM-DD HH:mm');
                        }else{
                            return '';
                        }
                    }
                },
                {"data": "total"},
                {"data": "proportion"},
            ],
            "language": {
                url: '/public/static/assets/pages/Chinese.json'
            },
            "pageLength": 10,
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function() {
                var nTrs = table3.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });
        tableSearch(table3, {'userId':userId});
        isLoadTable3 = true;
    };

    var data_table4 = function () {
        table4.dataTable({
            "processing": true,
            "ordering": false, // 禁止排序
            serverSide: true,// 开启服务器模式
            "ajax": {
                url: usercommission_url,
                dataType: 'json',
                "complete":function(data){
                    var data = jQuery.parseJSON(data.responseText);
                    $('#usercommission').find('.total_num').empty().html('累计提现次数：'+data.recordsTotal+' 次&nbsp;&nbsp;累计提现金额：'+data.totalNum+' 元');
                }
            },
            "autoWidth": false,
            "columns": [
                {
                    "data": "create_time",
                    "render": function(data, type, row, meta) {
                        data = new Date(data * 1000);
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    "data": "amount",
                    "render": function(data, type, row, meta) {
                        return data / 100;
                    }
                },
                {"data": "account"},
                {"data": "tradeNo"},

                {
                    "data": "update_time",
                    "render": function(data, type, row, meta) {
                        data = new Date(data * 1000);
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                }
            ],
            "language": {
                url: '/public/static/assets/pages/Chinese.json'
            },
            "pageLength": 10,
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function() {
                var nTrs = table4.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });
        tableSearch(table4, {'userId':userId});
        isLoadTable4 = true;
    };

    var tableSearch = function(table, params) {

        table.DataTable().search(JSON.stringify(params)).draw();
    };

    return {
        init: function() {
            _init();

            $('.nav-tabs').find('li').on('click', function () {
                var id = $(this).find('a').attr('data-id');
                switch(id){
                    case 'refundorder' :
                        if(!isLoadTable2){
                            data_table2();
                        }
                        break;
                    case 'ordercommission' :
                        if(!isLoadTable3){
                            data_table3();
                        }
                        break;
                    case 'usercommission' :
                        if(!isLoadTable4){
                            data_table4();
                        }
                        break;
                    default : break;
                }
            });
        }
    };

}();


$(function() {
    Memberinfo.init();

});
